package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"yktwo/models/mysql"
	"yktwo/pkg"

	"yktwo/yktworpc/internal/svc"
	"yktwo/yktworpc/yktworpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *RegisterLogic) Register(in *yktworpc.RegisterRequest) (*yktworpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	register := mysql.User{
		Model:    gorm.Model{},
		Mobile:   in.Mobile,
		Password: pkg.Md5(in.Password),
	}
	err := mysql.DB.Debug().Model(&register).Create(&register).Error

	if err != nil {
		return nil, fmt.Errorf("创建失败")
	}

	return &yktworpc.RegisterResponse{Id: int64(register.ID)}, nil
}
